/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.base.web.reader;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.base.entity.reader.BaseReader;
import com.thinkgem.jeesite.modules.base.service.reader.BaseReaderService;

/**
 * 基础管理_读者信息生成Controller
 * @author 呆子
 * @version 2020-02-21
 */
@Controller
@RequestMapping(value = "${adminPath}/base/reader/baseReader")
public class BaseReaderController extends BaseController {

	@Autowired
	private BaseReaderService baseReaderService;
	
	@ModelAttribute
	public BaseReader get(@RequestParam(required=false) String id) {
		BaseReader entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = baseReaderService.get(id);
		}
		if (entity == null){
			entity = new BaseReader();
		}
		return entity;
	}
	
	@RequiresPermissions("base:reader:baseReader:view")
	@RequestMapping(value = {"list", ""})
	public String list(BaseReader baseReader, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BaseReader> page = baseReaderService.findPage(new Page<BaseReader>(request, response), baseReader); 
		model.addAttribute("page", page);
		return "modules/base/reader/baseReaderList";
	}

	@RequiresPermissions("base:reader:baseReader:view")
	@RequestMapping(value = "form")
	public String form(BaseReader baseReader, Model model) {
		model.addAttribute("baseReader", baseReader);
		return "modules/base/reader/baseReaderForm";
	}

	@RequiresPermissions("base:reader:baseReader:edit")
	@RequestMapping(value = "save")
	public String save(BaseReader baseReader, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, baseReader)){
			return form(baseReader, model);
		}
		if(baseReader.getReaderId()!=null&&baseReader.getReaderId()!=""){
            baseReaderService.save(baseReader);
            addMessage(redirectAttributes, "保存读者信息成功");
            return "redirect:"+Global.getAdminPath()+"/base/reader/baseReader/list?repage";
        }else{
            if (baseReaderService.validReaderIdResult(baseReader)){
                addMessage(redirectAttributes, "保存读者信息失败,该读者已存在.请检查学号重新输入");
                return "redirect:"+Global.getAdminPath()+"/base/reader/baseReader/form?repage";
            }else if(baseReaderService.validReaderIDCardIdResult(baseReader)){
                addMessage(redirectAttributes, "保存读者信息失败,该读者已存在.请检查身份证号重新输入");
                return "redirect:"+Global.getAdminPath()+"/base/reader/baseReader/form?repage";
            }else {
                baseReaderService.save(baseReader);
                addMessage(redirectAttributes, "保存读者信息成功");
                return "redirect:"+Global.getAdminPath()+"/base/reader/baseReader/list?repage";
            }
        }


	}
	
	@RequiresPermissions("base:reader:baseReader:edit")
	@RequestMapping(value = "delete")
	public String delete(BaseReader baseReader, RedirectAttributes redirectAttributes) {
		baseReaderService.delete(baseReader);
		addMessage(redirectAttributes, "删除读者信息成功");
		return "redirect:"+Global.getAdminPath()+"/base/reader/baseReader/?repage";
	}

}